wordpress blog index is post page - php

I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?
Thanks!

This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.
To get the title, you have to first get the id of that page, and then you can pass it to a function:
<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>
Now you can print the $page_title and you should see "Journal".
Updated with Advanced Custom Fields
Now that you have the Journal page's id ($page_for_blog), you can get your field values with:
$field_value = get_field( 'field_name', $page_for_blog );
Obviously, replace 'field_name' with whatever field you're trying to retrieve.

Related

Get current page url on woocommerce taxonomy pages

I try to use <?php echo get_permalink(); ?> on a product Archive/Taxonomy page to get the current pages permalink/display the current pages permalink text, but it gets the permalink of the first product that shows up on the archive page instead of the current page's permalink. So instead of getting something like "/product-category/fashion/" it gets the first product that shows up on the list like /product/green-shirt,
How can I get the current product taxonomy page's permalink?
Found it! I apologize for the trouble!
<?php
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );
echo $current_url;
?>

Wordpress query users and display as taxonomy

I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.

wordpress/woocommerce make the 'theme_location' for pulling a menu equal the page title ID.

I want to insert a menu in my archive-product.php, however, I want this menu to be unique for every product page. I plan on accomplishing this by defining the menu name with the same name as the page title or page ID in woocommerce for my custom theme. and then setting my 'theme_location' => 'insert page title here' to equal the page title dynamically.
Currently in the archive-product.php I placed this code:
<?php
$args = array(
'theme_location' => '<?php woocommerce_page_title(); ?>'
);
?>
<?php wp_nav_menu( $args ); ?>
obviously '<?php woocommerce_page_title(); ?>' does not workdue to it being wrapped in single quotes. Is there any argument I can use to return the value of the page title? I've tried <?php echo get_the_title(); ?> as well, however, same story; it is PHP wrapped inside single quotes which is seen as a text string instead code.
PS I am receptive to different methods to accomplishing this goal if this approach is simply not optimal.
If your problem are the quotes, just call it with "false" as parameter, and it will return the page title instead of echoing it. So you will have:
'theme_location' => woocommerce_page_title(false)
http://docs.woothemes.com/wc-apidocs/function-woocommerce_page_title.html
But if I were you, i'd register in functions.php all my sidebars dynamically. First, use get_posts() to fetch all the pages, then foreach page grab its title and register a sidebar with that title.

Advanced Custom Fields after the main content

I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );

Wordpress: include content of one page in another

How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.

Categories