So I have had a little search and maybe I am searching for the wrong thing. I am trying to run a function from my functions.php file in wordpress and assign the returned array to a variable.
When I run the function, it just echos out the returned data rather than assigning it to the variable.
I'm assuming this is a Wordpress thing.
Code from functions.php
function get_portfolioBlock( ) {
$portfolio_query = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 1, 'name' => 'test1'));
if ( $portfolio_query->have_posts() ) {
while ($portfolio_query->have_posts() ) {
$portfolio_query->the_post();
$portfolio_block = array('title' => the_title(), 'excerpt' => the_excerpt() );
}
return $portfolio_block;
} else {
return 'error';
}
}
The code in my template file
<?php $portfolio = get_portfolioBlock(); ?>
When the page loads it automatically loads the data into the page without assigning the variable for me to use else where
Thank you Gerald!
The reason for it echoing in out the function was because when I was assigning the_title and the_excerpt I should have been using
get_the_title() and get_the_excerpt()
Related
Please Help
I want to make a search page for images stored in media gallery of wordpress. This page contains a textbox in which user will enter the number of image. The matched image should display in the different tab of browser.
I made a raw html page in wordpress where i put the which display a textbox but don't understand that where i have to write the php code to extract the images and how to show them.
Please give step by step instructions to solve the issue because i am beginner in wordpress.
You can create a custom function in functions.php for this and you can call that in your template
//In functions.php
if( ! ( function_exists( 'get_media_from_name' ) ) ) {
function get_media_from_name( $postName ) {
$args = array(
'name' => $postName,
'posts_per_page' => -1,
'post_type' => 'attachment',
);
$attachment = new WP_Query( $args );
if ( $attachment)
return $attachment;
else
return false;
}
}
//In you template where you can call that function
$get_all_matched_attachments = get_media_from_name( $postName );
if ( $get_all_matched_attachments ) {
foreach($get_all_matched_attachments as $get_all_matched_attachment){
echo '<pre>';print_r($get_all_matched_attachment);
}
}
I've tried everything I can think of. the title spits out at the top of the content wrapper and the html tags stay where they should.
I'm performing this shortcode from a page. I need the title and perma link to an image in the media library so that I can spit out html to show it on a page via shortcode
add_shortcode( 'ispimg', 'isp_gallery_item' );
function isp_gallery_item( $atts ) {
// Attributes
$a = shortcode_atts( array(
'id' => '',
), $atts );
$isp_post_id = get_post($a['id']);
setup_postdata($isp_post_id);
$pt = the_title();
return "<h3>".$pt."</h3>";
wp_reset_postdata();
}
Not totally sure if this is your issue, but I suggest you not to mess with the main query, you may be affecting other parts of your page because of that.
Use the API functions like get_the_title() instead:
function isp_gallery_item( $atts ) {
// Attributes
$a = shortcode_atts( array(
'id' => '',
), $atts );
return sprintf( '<h3>%s</h3>', get_the_title( $a['id'] ) );
}
add_shortcode( 'ispimg', 'isp_gallery_item' );
Or in case you need the loop, take a look at the WP_Query class.
So I want to apply two separate templates, to two different types of pages (not posts).
Both page types are children pages.
The template 'exhibition-template.php' is applied to all pages that are children of the 'archive' page.
This works!
I do this with a function in functions.php that tests to see if a page is a child of 'archive', then use that function in page.php to apply the template:
//////////////////////////////////////////////
function is_archivechild() {
// Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
// Get the page as an Object
$archive = get_page_by_title('archive');
// Filter through all pages and find archive's children
$archive_children = get_page_children( $archive->ID, $all_wp_pages );
if ($archive_children > 0){
return true;
}
else{
return false;
}
}
//////////////////////////////////////////////
And using it in page.php:
//////////////////////////////////////////////
elseif ( is_archivechild() ) {/*a function made up in functions.php, tests if it is a of archive*/
get_template_part( 'exhibition-template' );
get_template_part( 'normalfooter' );
}
//////////////////////////////////////////////
The problem is when I write the function a second time with new variables, and try to implement it in the exact same way except for applying 'artist-template.php' to children pages of 'artists', nothing changes, in fact it still applies 'exhibition-template.php' no matter what.
//////////////////////////////////////////////
function is_artistschild() {
// Set up the objects needed
$artist_query = new WP_Query();
$all_wp_pages = $artist_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
// Get the page as an Object
$artists = get_page_by_title('artists');
// Filter through all pages and find artists's children
$artists_children = get_page_children( $artists->ID, $all_wp_pages );
if ($artists_children > 0){
return true;
}
else{
return false;
}
}
//////////////////////////////////////////////
elseif ( is_artistchild() ) {/*a function made up in functions.php, tests if it is a CHILD page, aka an exhibition*/
get_template_part( 'artist-template' );
get_template_part( 'normalfooter' );
}
//////////////////////////////////////////////
Whaaaaat the hell is going on?!?!?
much thanks!
You can write inside page.php the above:
<?php global $post;
$page = get_page_by_title( 'archive' ); // page title , Archive in your case . Try an alternative page name.
$page_parent_ID=$page->ID;
?>
<?php if ($post->post_parent == $page_parent_ID) {
get_template_part( 'exhibition-template' ); // you name of template you want to appear
}
?>
Hoping you can help me figure out why I can't get get_first_post_in_category to successfully return a usable post ID. At the moment I am using the Wordpress Plugin Boilerplate to create a plugin that removes the first post of a specified category (through the admin options page) from the primary loop. All is well until I try and programmatically retrieve the post ID.
Here I define my hook (works fine):
// In private function define_public_hooks()
$this->loader->add_action( 'pre_get_posts', $plugin_public, 'exclude_featured_post_pre_injection');
Here is the callback function I am using to exclude a specific post from the main query. If $first_post_in_category_id is manually set, it works perfectly. If I try and set it to " = get_first_post_in_category();" I am thrown a 500 error.
public function exclude_featured_post_pre_injection($query){
// Doesn't work
$first_post_in_category_id = $this->get_first_post_in_category();
//Works
// $first_post_in_category_id = '427';
if ($query->is_home() && $query->is_main_query()) {
$query->set('post__not_in', array($first_post_in_category_id));
}
}
And here is the issue ::: this function I am attempting to just return the ID of the first post in the category to be used in other action/filter callbacks. If I call this, it returns a 500 error.
public function get_first_post_in_category(){
$cat_id = get_option('sticky_content_category_id');
// currently returns (string) '3' which has posts in it
$args = array(
'posts_per_page' => 1,
'cat' => $cat_id
);
$latest_cat_post = new WP_Query($args);
while ( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();
$first_post_id = the_ID();
endwhile;
wp_reset_postdata();
return $first_post_id;
}
Any idea on the best way to resolve the issues I am running into?
I cant see how it can cause an error 500 but the code below works for me.
$category_id = get_cat_ID('Wordpress');
//echo $category_id;
$args = array(
'posts_per_page' => 1,
'cat' => $category_id,
'orderby' => 'ID',
'order' => 'DESC'
);
$query = new WP_Query($args);
while($query->have_posts()):$query->the_post();
echo the_ID();
endwhile;
I'm studying PHP and have a basic question,
I have this code in the page portfolio.php
<?php
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
if( $portfolio_query->have_posts() ) :
echo '<div id="primary" class="hfeed">';
while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
// project url
$portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
if( !empty($portfolio_url) )
$portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);
?>
I would like to echo $portfolio_url in my function.php
Any reference for more study in this case would be appreciated.
It's not clear what you're trying to do, but if you need to pass a url to a function then you can do so with an argument:
function your_function($url) {
// Do whatever you want with $url here, like:
echo 'A link';
}
You can save the above into functions.php.
Based on your comments, I think what you're trying to do is take your code in portfolio.php and turn it into a function that you can call elsewhere. In that case you need to actually define it as a function:
function get_portfolio_url() {
// Insert your existing code in here...
// Then you can either echo $portfolio_url:
echo $portfolio_url;
// Or you can just return it if you plan on doing other things with it
return $portfolio_url
}
You can insert the above function into functions.php. Then you can call it anywhere with get_portfolio_url(). Technically you would no longer require portfolio.php, since you've created a function that does exactly the same thing. Be aware that you don't need to create separate PHP files for every function you require; you can just define them as separate functions in one location and then call them as necessary (which is essentially what functions.php is for in WordPress; a single file to hold all the functions your theme uses).