Understanding wordpress the_content - mysql

I have been asked to tweak a friend's website. The website was built for them using Wordpress by an agency they no longer work with. Problem is, I don't know very much about Wordpress, so I have some basic questions…
I can see (by comparing the source as viewed in my browser to the php templates that I can edit through the Wordpress interface) that the elements I need to modify are generated by the page's call to "the_content()". I don't really understand what this function does, but I think it pulls content for a given page from the MySQL database. Is that right?
I suspect that the Wordpress interface alone won't be enough to let me modify elements that come out of that database. Is that correct? How does anyone change, for example, the specific arrangement of text and images on a page if the relevant markup is fetched by "get_content()"?

When developing for WordPress or even just tweaking, it's really important to get at least a general understanding of the "loop" and how we use it for output. In order to output the content you need to call the method within the loop. If it's not within the loop it won't get inserted into your page.
(It works this way with all of your pages... index.php, page.php, single.php, post.php, etc... Your methods need to be placed within the loop in order for them to display within that page.)
Here's a simple example of the way the loop is used to output the title and content of posts:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // loop start
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?> // loop end
<p>Sorry, this page does not exist</p>
<?php endif; ?>
If you take a look at any standard WordPress theme (take Twenty Fourteen, for example), you'll find that the_content() is output together with calls to other elements (in this case - the_title()). If you want to want to change the arrangement of the specific elements within your page, just modify the order as they are within your loop, or check the WordPress Codex for additional options/methods.
Here's a short list of some other things you can display:
next_post_link – Displays a link to the post published chronologically after the current post.
previous_post_link – Displays a link to the post published chronologically before the
current post.
the_category – Displays the category or categories associated to the post or page being viewed.
the_author – Displays the author of the post or page.
the_content – Displays the main content for a post or page.
the_excerpt – Displays the first 55 words of a post’s main content then with a [...] or read more link that goes to the full post. The length of excerpts can be controlled by using this slightly advanced method or by using the Excerpt field on the post edit page.
the_ID – Displays the ID for the post or page.
the_meta – Used to display custom fields.
the_shortlink – Displays a link to the page or post using the url of the site using
the ID of the post or page.
the_tags – Displays the tag or tags associated with the post.
the_title – Displays the title of the post or page.
the_time – Displays the time or date for the post or page. This can be customized using the standard php date function formatting.
If you want to further customize some of these methods, you can do that within functions.php. Otherwise, all styling is done within your styles.css.

Probably your content is being generated by a shortcode.
You can modify the content that wordpress fetches from the database with the_content filter:
// returns the content of $GLOBALS['post']
// if the page is called 'debug'
function my_the_content_filter($content) {
// assuming you have created a page/post entitled 'debug'
if ($GLOBALS['post']->post_name == 'debug') {
return var_export($GLOBALS['post'], TRUE );
}
// otherwise returns the database content
return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );
maybe its not the best solution but you can modify the content before it gets output.
source

Related

How to make a custom Archive page with Wordpress using custom post types and acf fields

I am building a child theme on GeneratePress to create a custom site for a client and am stuck on customizing the Archive pages. I've determined that it will be best to ignore the theme functionality and build the page loops from scratch. The custom archive should take the place of wp/generatepress' standard archive page and function in the same way by showing relevant posts when a taxonomy term is clicked. I need to be able to choose which html tags are used and style them with css. My issue is that I don't understand wordpress dev enough to figure out how to do this.
I have two custom post types, Research and Resources, as well as a bunch of different custom taxonomies that are applied to both or each post type. I'd like to customize the archive page (with archive templates if necessary) so it displays the appropriate Research and/or Resource files when visited.
I understand that I need to loop through all the posts, but I do not know how to grab only the relevant ones per the given archive page.
Examples of loops with example content will be the most helpful. Thank you!!
I chatted with someone on Reddit who kindly answered this question for me here.
They said:
So I wouldn't do it as a template personally (you could), I would do it through having two new files a archive-research.php & archive-resources.php.
Then if the loop is going to be the same on both pages, which I think you said it is going to be (you just want to display the posts from each custom post type), I would use a template part.
So create a new folder in your theme folder called inc or includes and name the file custom_post_loop.php (name can be anything).
Then in your archive page, call...
<?php get_template_part('inc/custom_post_loop');?>
In that file you are just wanting to write a simple post loop in there.
while ( have_posts() ) : the_post();
// Your loop code
// just so you can see this work I have called the title
the_title();
endwhile;
else : _e( 'Sorry, no posts were found.', 'textdomain' ); endif; ?>
https://developer.wordpress.org/reference/functions/have_posts/
Have a look at that link to find out more. But thats the direction to go...
If you really wanted to got the direction you have with the template, you would have to add arguments to your posts loop.
it works the same way but instead of archive.php its taxonomy.php...
However, you don't want a taxonomy-{taxonomy name}.php file for each taxonomy as that would be ridiculous unless you knew all the taxonomies haha and had really specific user cases for them...
so just create a taxonomy.php in your theme files and to make sure its working just add this code to it...
<?php get_header();>
<h1><?php the_archive_title();?></h1>
//then put your loop in from earlier...
<?php get_template_part('inc/custom_post_loop');?>
<?php get_footer();?>

Is there any way to show all the components data from /jcr:content/par/ location

I have a query regarding the data rendering of the different page at one place. As every page is build using many components and all the components data gets stored under jcr location of page ie. /jcr:content/par/{components list}. The data is properly rendering on this page.
Now I have a situation where I need to create a component to search the page(i.e unique product), if this product page is available in the repository, I need to render its data just under the search box. For this I am creating json which I will use to render the content after search found.
But if there is any other way i can include this component from the /par location of the page to just display the data as it is, rather than building json(of all the components data) and then reading it at the time of display.
I am wondering if we have any method to display all the components data by just including the /par/{components} on a page. This way I can speed up the development, and it looks faster way to display the content as well.
thanks in advance....
If you have static page, then you can go through list of search results (product resources) and include component, which renders product, for each of them. Like:
<c:forEach items="${productsList}" var="productPath">
<cq:include path="${productPath}" resourceType="/apps/you-project/components/product-component-name"/>
</c:forEach>
If you show results dynamically - then you can do ajax requests for product resources. Something like this:
var productHtml = CQ.shared.HTTP.get(productPath + ".html");
Or the same using JQuery. Then you can add html to your page.
However, with second approach you should add clientlibs from component /apps/you-project/components/product-component-name to search results page yourself, because they will not be loaded with ajax request.

Wordpress custom header issue

I have created a template in wordpress for the home page, named: page-home-slider.php
I have chosen the home page to take that custom template.
Until here everything works fine.
Then I have created header-home.php, so a custom header for my home page.
Then in page-home-slider.php I have coded: get_header("home");
Now, when I access the home page from browser, the default header is displayed first and under it, my custom header. Is this normal? I wanted to have just my custom header. Please tell me what I am doing wrong.
PS: I am using JobRoller template if it matters somehow.
As discussed in the comments, you could set a condition in header.php that checks if the requested page has the template using the is_page_template() function:
header.php
if ( is_page_template( 'page-home-slider.php' ) )
{
// do something different
}
Still, you need to check where get_header() is getting called to avoid duplicates.
Reference: https://developer.wordpress.org/reference/functions/is_page_template/

Using multiple layout in Yii

My Yii application has a particular section called 'Edit Profile' . It's a pretty data heavy section in the way a lot of data is pulled from db for this one .
However the problem is that I have tab pagination in this section . Because only this section uses tabs on the website I did not include the related CSS/Javascript files in the main layout header .These have been referenced in the view file itself . Because of this the tabs takes time to show up and the tab titles appear as a list first (for a second or two) and then get distributed into tabs with the correct UI . This is of course unacceptable behaviour . Is there any way to selectively include related js/css files into the header tag for this particular view or should I include it in main layhout file even though it won't be used in a lot of other places on the website thus possibly slowing down other pages .
Just specify the position for the file.
In your view where you are including the js or css :
// for js
Yii::app()->clientScript->registerScriptFile('url_of_file',CClientScript::POS_HEAD);
// for css
Yii::app()->clientScript->registerCssFile('url_of_file');
Recommended documentation: registerScriptFile() , and registerCssFile()
You can create your own layout in the layouts folder , where u can include all the required jquery scripts and css. U can then call your layout in the controller.
public function actionDoSomething(){
$this->layout = 'mylayout';
$this->render('myview');
}

How to display blog post in wordpress?

I have a wordpress theme "Hatch", and I'm doing it for my photography. I usually do websites with HTML/CSS (in Dreamweaver), and this is the first time doing Wordpress.
In My homepage, you can see the recent posts as thumbnails. I'm thinking of creating a new menu, called 'Blog', basically just like what normal themes do, displaying blog posts. It might be something simple, but i just can't find what to code to make the posts display as normal display .
the website is lizettephotography.com
thanks heaps!
Liz
No need to code.
Create new category "Blog". Add that category to your main menu.
After that add new post to Blog category. on click blog menu link will show all post having blog category.
You need to format post design as you want.
EDIT :
In order to activate “post formats” in WordPress 3.1+, you will need to open your theme’s functions.php file and paste the following code:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
Note: aside, and gallery are not the only available post formats. The available list of post formats are:
aside – Typically styled blog format.
chat – A chat transcript.
gallery – A gallery of images.
link – A link to another site.
image – A single image.
quote – A quotation.
status – A short status update, usually limited to 140 characters. Similar to a Twitter status update.
video – A single video.
For the full list of post formats, refer to WordPress Codex.
Once you have added this code, you will see a new field in your post write panel in the right hand column where you see publish.
Upon writing the post, you can change the format and hit publish. This will allow you to display your post in a pre-styled format.
Edit your post loop.
Suppose in your case blog category post format is asid
We are going to be utilizing the conditional tag: has_post_format()
if ( has_post_format( 'aside' ) {
// Blog Category format
}
else
{
// Normal Formate
}
I hope this will help you. More Info...