I think this question is php rather than WordPress...
I have a block of code that outputs pages which use a specific WordPress page template:
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-product.php'
));
foreach($pages as $page){
echo $page->post_title.'<br />';
}
My question is how do I wrap the post_title in a link (the_permalink() to the page itself)?
You would do the following:
foreach ( $pages as $page ){
printf( '%s<br />', get_permalink( $page ), $page->post_title );
}
Try this..
get_page_link( $page->ID )
In your code..
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-product.php'
));
foreach($pages as $page){
echo '' . $page->post_title.'<br />';
}
For more information Wordpress always has lots of example and information on what your using.
In this case I used actual code provided by Wordpress themselves, thus will be best way to do this. Please see https://codex.wordpress.org/Function_Reference/get_pages
Try this snippet:
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-product.php'
));
foreach($pages as $page){
///Do not forget to escape the title
echo '' . esc_html($page->post_title) . '';
}
Thanks for the suggestions. bodi0 - I liked the esc_html, thanks. I finally figured it out slightly differently with this:
<?php
$studentpages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'students.php'
));
foreach($studentpages as $studentpage){
$studentlinks = get_page_link($studentpage->ID);
echo ''.$studentpage->post_title.'' . '<br />';
}
?>
Related
i iam creating a section with tags with this code, is a function to retrieve tags and exclude some tags also,
$args = array('name__like' => $name_like, 'exclude' => array(75,177,42,74,197,36,40,140,162,108,86,47,4,29,22,215,87,151,104),'order' => 'ASC');
$tags = get_tags( $args );
if ( !empty( $tags ) && !is_wp_error( $tags ) ) {
$count = count($tags);
$i=0;?>
<ul class="my_term-archive">
<?php
foreach ($tags as $tag) {
$i++;
$tag_link = get_tag_link( $tag->term_id );
$tag_id = get_tag_ID($tag->name);
if(strtolower(substr($tag->name,0,1)) !=$name_like){
continue;
}
//i need a function here to retrieve images with the id of the tag
//attached
//////
$html .= "<li><a href='{$tag_link}' id='{$tag_id}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
}
echo $html;
?>
</ul>
then i put this code in my functions.php file in wordpress, to make avaliable the tag box in the picture managment, so i can tag pictures now,
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
so my question is how can find and display the images by the id tag ?
sorry my bad english, is not my native lenguage. any help is very welcome. thanks
You can actually handle this with a basic WP_Query call. There's lots of details and options for the WP_Query object here, but I'd think you could do something like this:
$args = array(
'post_type' => 'attachment',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'whatever-your-tag-slug-is',
),
),
);
$query = new WP_Query( $args );
I cannot get working my code and I thought that best way is asking here.
so as on the title
Im looking to create a function (link for a specific template file) for my template-contact.php
Tried to do with this function wich I found in another topic but it didnt work.
function get_contact_page() {
$contact_page = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template-contact.php'
)
);
$contact_id = $showcase_page[0]->ID;
echo get_permalink( $contact_id );
}
but when I use
<a href="<?php echo get_contact_page(); ?>" class="widget" data-toggle="tooltip" data-placement="bottom" data-title="CONTATTO"><span
class="ico ico-phone-btn"></span></a>
does nothing.
Thanks for all your help
get_pages() has an issue with meta_key and meta_value parameters. I recommend using WP_Query instead:
function get_contact_page() {
$query = new WP_Query(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template-contact.php',
'fields' => 'ids',
)
);
return $query->have_posts() ? get_permalink( $query->posts[0] ) : '#';
}
Try
echo get_permalink( $archive_id );
change to
return get_permalink( $archive_id );
I am working on a Wordpress-Design and i want to creat a Custom Menu.
$items = wp_get_nav_menu_items('Menu', array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false));
echo '<pre>'; print_r($items); echo '</pre>';
foreach($items as $item){
echo '<div class="menu_entry">'.$item->title.'</div>';
}
The problem is, i need the "current-page"-Class, which is WordPress creating - in the Standard Menu.
Any Ideas how to add this class?
Solution time:
WordPress's function that adds these classes is _wp_menu_item_classes_by_context(). This is called already when you use wp_nav_menu but not wp_get_nav_menu_items. Fortunately, the latter provides a filter so we can do it ourselves:
add_filter( 'wp_get_nav_menu_items', 'prefix_nav_menu_classes', 10, 3 );
function prefix_nav_menu_classes($items, $menu, $args) {
_wp_menu_item_classes_by_context($items);
return $items;
}
You can do a compare on the current page / cat etc ID against the menu items object_id which is the ID of the page / category etc its linked to.
Something like (untested);
global $post;
$thePostID = $post->ID;
foreach($items as $item){
if($thePostID === $item->object_id) {
echo '<div class="menu_entry">'.$item->title.'</div>';
}else{
echo '<div class="menu_entry">'.$item->title.'</div>';
}
}
Using the function get_queried_object_id(). This works fine in all the pages, including the Blog page.
See an example:
if ( $menu_items = wp_get_nav_menu_items( 'menu' ) ) {
foreach ( $menu_items as $menu_item ) {
$current = ( $menu_item->object_id == get_queried_object_id() ) ? 'current' : '';
echo '<li class="' . $current . '">' . $menu_item->title . '</li>';
}
}
I'm trying to make a Wordpress plugin for my blog that scans for posts that contain the custom field _videoembed. I've got everything made and it activates correctly but I receive a PHP error when I open up a page to test it out:
Fatal error: Call to a member function get_results() on a non-object in .../wp-content/plugins/youtubesubscription/videos.php on line 26.
Does anyone know enough about PHP to help me out? Here's my plugin code (pasted on pastebin because of size):
http://pastebin.com/uaEWjTn2
EDIT 1
I'm no longer getting any errors after inserting global $wpdb; but now nothing's showing up. Here's my updated code: http://pastebin.com/R2ZuEknY. Note, this code also incorporates auto YouTube thumbnails, which were obtained from a function that trims YouTube URLs to the IDs (link).
EDIT 2
Got it working, turns out all I needed to do was insert '_videoembed' as the 'meta_key' argument for a wp_query. Here's my working code below:
<?php
$args = array(
'meta_key' => '_videoembed',
'post_status' => 'publish',
'posts_per_page' => '' . $number . '',
'order' => 'date'
);
query_posts( $args );
while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$VIDEOID = ytvideoID($post->ID);
?>
<li onClick="window.location.href='<?php the_permalink(); ?>'">
<?php
global $post;
$videoId = ytvideoID($post->ID);
$videoInfo = parseVideoEntry($videoId);
echo '<a href="'.get_permalink().'">';
echo '<div class="image">';
echo '<img src="http://img.youtube.com/vi/'.$VIDEOID.'/default.jpg">';
echo '<div id="time" style="position:absolute;z-index:9;bottom:2px;right:2px;font-size:10px;color:#fff;background:#000;padding:0px 2px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;opacity:0.75;">'.hms($videoInfo->length).'</div>';
echo '</div>';
echo '<h4>'.get_the_title().'</h4>';
echo '<div id="description">';
echo '<div id"views"><h3>'.number_format($videoInfo->viewCount).' Views</h3></div>';
echo '<div class="singleauthor"><h3>by '.$videoInfo->author.'</h3></div>';
echo '</div>';
echo '</a>';
?>
</li>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
you get the post by custom field using the meta_query.
$args= array(
'category_name' => 'courses',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'front_page',
'value' => 'yes',
'compare' => 'LIKE',
))
);
$the_query = new WP_Query( $args );
please refer my tutorial for more details.
http://www.pearlbells.co.uk/filter-posts-custom-fields-wp_query/
You'll have to to add global $wpdb;
try replacing
function mbrecentvids()
{ ?>
<?php
with
function mbrecentvids()
{
global $wpdb;
It looks like you're missing a semi-colon on line 26.
I see:
$pageposts = $wpdb->get_results($querydetails, OBJECT_K)
I need to display all the pages on the one page.
At the moment I am using this to bring in each page:
<?php
$page_id = 5; //id example
$page_data = get_page( $page_id );
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
?>
But if a new page is created it wont display it as wont have this code and its ID in place..
Is there a way to bring all the pages in automatically?
Any help will be appreciated, thanks
You can get all the pages of your blog by using get_pages();, do this in a loop like so:
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}
The accepted answer's returned array doesn't contain "post customs" (which I needed in my case). And also you may need to retrieve pages with specific attributes. Use args for more customized results:
<?php $args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
?>
And use them like this:
<?php
foreach ($pages as $page) {
$title = $page->post_title;
echo $title. "<br />";
} ?>
Read more here
Also, if you are using the Twenty Ten theme (I don't know how will it work in others) you can easily get all the pages formatted:
echo "<h1 class=\"entry-title\">".$title."</h1>"
."<div class=\"entry-content\">".$content."</div>";
Well you can get the contents of each page by this code as well.
$content = apply_filters( 'the_content', $content );
Still there is huge problem if you are using custom templates for different pages you can get the content but not the template files on your one page